package com.baizhi.service.impl;

import com.baizhi.dao.CmfzUserDao;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.CmfzUserService;
import com.baizhi.util.ThreadPollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;


@Service
public class CmfzUserServiceImpl implements CmfzUserService {
    @Autowired
    private CmfzUserDao cmfzUserDao;
    private Logger logger= LoggerFactory.getLogger(CmfzUserServiceImpl.class);

    @Override
    public Map queryAll(Integer page, Integer rows) {
        Map map = new HashMap();

        IPage<CmfzUser> iPage = new Page<>(page, rows);
        QueryWrapper<CmfzUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_status", 0);
        IPage<CmfzUser> cmfzUserIPage = cmfzUserDao.selectPage(iPage, wrapper);
        List<CmfzUser> records = cmfzUserIPage.getRecords();
        long total = cmfzUserIPage.getTotal();

        map.put("total", total);
        map.put("rows", records);

        return map;
    }

    @Override
    public void insertMany(List<CmfzUser> cmfzUsers) {
        cmfzUserDao.insertMany(cmfzUsers);
    }

    @Override
    public List<Map> queryCountBySex() {
       /* Map map = new HashMap();
        List<Map> maps = cmfzUserDao.queryCountBySex();
        for (Map m : maps) {
            map.put(m.get("userSex"), m.get("sexCount"));
        }*/

        List<Map> maps = cmfzUserDao.queryCountBySex();
        return maps;
    }

    @Override
    public List<Map> queryCountByProvince() {
        List<Map> maps = cmfzUserDao.queryCountByProvince();
        return maps;
    }

    @Override
    public  List<Map<String, Integer>> queryCountByTime() {
       /* Map map = new HashMap();
        List<Map<String, Integer>> maps = cmfzUserDao.queryCountByTime();
        for (Map m : maps) {
            map.put(m.get("userMonth"), m.get("monthCount"));
        }*/

        List<Map<String, Integer>> maps = cmfzUserDao.queryCountByTime();
        logger.debug("@@@@@@@@@@@@@@@@@@@@@@@@@{}",maps);
        return maps;
    }


    @Override
    public Map queryCountByThread() {
        Map map = new HashMap();

        /*创建任务计数器，为线程总数*/
        CountDownLatch countDownLatch = new CountDownLatch(3);


        /*通过线程池获取线程，提交根据性别查询的任务*/
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps1 = queryCountBySex();
                map.put("maps1",maps1);

               /* List<Map> maps = cmfzUserDao.queryCountBySex();
                for (Map m : maps) {
                    map.put(m.get("userSex"), m.get("sexCount"));
                }*/

                /*计数器减一*/
                countDownLatch.countDown();
            }
        });

        /*通过线程池获取线程，提交根据省份查询的任务*/
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps2 = queryCountByProvince();
                map.put("maps2",maps2);

               /* List<Map> maps = cmfzUserDao.queryCountByProvince();
                map.put("maps", maps);*/
                /*计数器减一*/

                countDownLatch.countDown();
            }
        });

         /*通过线程池获取线程，提交根据注册时间查询的任务*/
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map<String, Integer>> maps3 = queryCountByTime();
                map.put("maps3",maps3);

                /*List<Map<String, Integer>> maps = cmfzUserDao.queryCountByTime();
                for (Map m : maps) {
                    map.put(m.get("userMonth"), m.get("monthCount"));
                }*/

                /*计数器减一*/
                countDownLatch.countDown();
            }
        });

        try {
            /*await当计数器为0的时候，主线程向下执行*/
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map;
    }



    @Override
    public Map queryCountByFuture(){
        HashMap map = new HashMap();

        /*根据性别查询*/
        Future<Boolean> future1 = ThreadPollUtil.executor.submit(new
             Callable<Boolean>() {
                 @Override
                 public Boolean call() throws Exception {

                     List<Map> maps1 = queryCountBySex();
                     map.put("maps1",maps1);

                    /* List<Map> maps = cmfzUserDao.queryCountBySex();
                     for (Map m : maps) {
                         map.put(m.get("userSex"), m.get("sexCount"));
                     }*/
                     return true;
                 }
             });

         /*根据省份查询*/
        Future<Boolean> future2 = ThreadPollUtil.executor.submit(new
            Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {

                    List<Map> maps2 = queryCountByProvince();
                    map.put("maps2",maps2);

                  /*  List<Map> maps = cmfzUserDao.queryCountByProvince();
                    map.put("maps", maps);*/

                    return true;
                }
            });

         /*根据时间查询*/
        Future<Boolean> future3 = ThreadPollUtil.executor.submit(new
             Callable<Boolean>() {
                 @Override
                 public Boolean call() throws Exception {
                     List<Map<String, Integer>> maps3 = queryCountByTime();
                     map.put("maps3",maps3);

                    /* List<Map<String, Integer>> maps = cmfzUserDao.queryCountByTime();
                     for (Map m : maps) {
                         map.put(m.get("userMonth"), m.get("monthCount"));
                     }*/
                     return true;
                 }
             });


        /*如果所有的线程都正常的执行完毕，返回*/
        try {
            if(future1.get()&&future2.get()&&future3.get()){
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
